package com.hongxun.design.ui.headandbottonpanel;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.CollectActivity;
import com.hongxun.design.activitys.ProjectSearchActivity;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.fragments.EncyclopediasFragment;
import com.hongxun.design.fragments.ProjectFragment;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.FragmentUtil;
import com.hongxun.design.utils.InputUtil;
import com.hongxun.design.utils.PopwindowUtil;

//import peiwang.peiwang.R;

//import Constant;

public class HeadControlPanel extends RelativeLayout {

    private Context mContext;
    private EditText search_input;
    private TextView mleft_title,home_left_icon;
    private TextView mRightTitle, search_icon, search_text;
    private LinearLayout search_title;
    private HeadControlPanel midle_bar;
    private static final float middle_title_size = 20f;
    private static final float right_title_size = 17f;
    private static final int default_background_color = Color.rgb(255, 255, 255);

    public HeadControlPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mRightTitle = (TextView) findViewById(R.id.right_title);
        mleft_title = (TextView) findViewById(R.id.left_title);
        home_left_icon = (TextView) findViewById(R.id.home_left_icon);
        search_title = (LinearLayout) findViewById(R.id.search_title);
        search_icon = (TextView) findViewById(R.id.search_icon);
        search_input = (EditText) findViewById(R.id.search_input);
        search_text = (TextView) findViewById(R.id.search_text);
        search_input.setVisibility(GONE);
        home_left_icon.setVisibility(GONE);
        search_text.setVisibility(VISIBLE);
        SetTypeface.set(search_icon, IconSourse.SEARCH);//搜索
        midle_bar = (HeadControlPanel) findViewById(R.id.midle_bar);
        setBackgroundColor(default_background_color);
    }

    public void initHeadPanel() {
        if (mleft_title != null) {
            setMiddleTitle(Constant.FRAGMENT_FLAG_HOME);
        }
    }

    ProjectFragment projectFragment;
    EncyclopediasFragment encyclopediasFragment;

    public void setMiddleTitle(String s) {
        search_title.setOnClickListener(null);
        mleft_title.setOnClickListener(null);
        mRightTitle.setOnClickListener(null);
        home_left_icon.setVisibility(GONE);
        if (s.equals(Constant.FRAGMENT_FLAG_MY)) {
            midle_bar.setVisibility(GONE);
        } else {
            midle_bar.setVisibility(VISIBLE);
            switch (s) {
                case Constant.FRAGMENT_FLAG_HOME:
                    //首页
                    mRightTitle.setTextSize(20);
                    mleft_title.setText(R.string.app);
                    mleft_title.setTextColor(getResources().getColor(R.color.bar_left));
                    mRightTitle.setTextColor(getResources().getColor(R.color.bar_right));
                    SetTypeface.set(mRightTitle, IconSourse.ADD);
                    home_left_icon.setVisibility(VISIBLE);
                    if (MyApp.isOnLine){
                        home_left_icon.setText("在线");
                        home_left_icon.setBackgroundResource(R.drawable.circular_green_4);
                    }else{
                        home_left_icon.setText("离线");
                        home_left_icon.setBackgroundResource(R.drawable.circular_gray_4);
                    }

                    search_title.setVisibility(GONE);
                    mRightTitle.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            PopwindowUtil.openPop(mRightTitle, (Activity) getContext(), DataUtil.getPopItem());
                        }
                    });
                    break;
                case Constant.FRAGMENT_FLAG_PROJECT:
                    //项目

                    mRightTitle.setTextSize(20);
                    mleft_title.setTextColor(getResources().getColor(R.color.bar_right));
                    mRightTitle.setTextColor(getResources().getColor(R.color.bar_right));
                    SetTypeface.set(mRightTitle, IconSourse.ADD);
                    search_title.setVisibility(VISIBLE);
                    search_icon.setVisibility(VISIBLE);
                    /**搜索界面**/
                    search_title.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            search();
                        }
                    });
                    /**pop**/
                    mRightTitle.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            PopwindowUtil.openPop(mRightTitle, (Activity) getContext(), DataUtil.getPopItem());
                        }
                    });
                    mleft_title.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            InputUtil.closeInput((Activity) mContext);
                            projectFragment = FragmentUtil.getIntence((FragmentActivity) mContext).projectFragment;
                            if (projectFragment == null) return;
                            if (projectFragment.type.equals(MString.LIST)) {//变为map
                                projectFragment.type = MString.MAP;
                                SetTypeface.set(mleft_title, IconSourse.LIST);
                                search_input.setEnabled(true);
                                search_input.setVisibility(VISIBLE);
                                search_text.setVisibility(GONE);
                                search_input.setOnKeyListener(new OnKeyListener() {
                                    @Override
                                    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                                        if (keyCode == KeyEvent.KEYCODE_ENTER) {//修改回车键功能
                                            // 先隐藏键盘
                                            InputUtil.closeInput((Activity) mContext);
                                            //跳转到搜索结果界面
                                            if (projectFragment.type.equals(MString.MAP)) {
                                                projectFragment.search_Map(((EditText) view).getText().toString());
                                            }
                                        }
                                        return false;
                                    }
                                });
                                search_title.setOnClickListener(null);
                            } else {
                                // 变为list
                                search_text.setVisibility(VISIBLE);
                                search_input.setVisibility(GONE);
                                projectFragment.type = MString.LIST;
                                SetTypeface.set(mleft_title, IconSourse.DINGWEI);
                                search_title.setOnClickListener(new OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        search();
                                    }
                                });

                            }
                            projectFragment.changeStyle();
                        }
                    });

                    projectFragment = FragmentUtil.getIntence((FragmentActivity) mContext).projectFragment;
                    if (projectFragment != null){
                        if (projectFragment.type.equals(MString.LIST)) {//变为map
                            search_text.setVisibility(VISIBLE);

                            search_input.setVisibility(GONE);
                            SetTypeface.set(mleft_title, IconSourse.DINGWEI);
                            search_title.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    search();
                                }
                            });
                        }else{
                            search_input.setEnabled(true);
                            search_input.setVisibility(VISIBLE);
                            search_text.setVisibility(GONE);
                            SetTypeface.set(mleft_title, IconSourse.LIST);
                            search_title.setOnClickListener(null);
                        }
                    }

                     break;
                case Constant.FRAGMENT_FLAG_ENCYCLOPEDIAS:
                    //百科
                    mRightTitle.setTextSize(14);
                    search_title.setVisibility(VISIBLE);
                    search_icon.setVisibility(GONE);
                    mleft_title.setTextColor(getResources().getColor(R.color.login_yellow));
                    mRightTitle.setTextColor(getResources().getColor(R.color.login_yellow));
                    SetTypeface.set(mleft_title, IconSourse.Favorites);//搜索
                    SetTypeface.set(mRightTitle, IconSourse.SEARCH);//搜索
                    mRightTitle.append(" 搜索");
                    search_input.setEnabled(true);
                    search_input.setVisibility(VISIBLE);
                    search_input.setText("");
                    search_text.setVisibility(GONE);
                    mRightTitle.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            encyclopediasFragment = FragmentUtil.getIntence((FragmentActivity) mContext).encyclopediasFragment;
                            if (encyclopediasFragment == null) return;
                            encyclopediasFragment.onRefresh();
                        }
                    });
                    mleft_title.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle bundle = new Bundle();
                            bundle.putString(MString.TITLE, search_input.getText().toString());
                            IntentToActivity.intent((Activity) mContext, CollectActivity.class, bundle);
                        }
                    });

                    break;

            }


        }

    }

    private void search() {
        IntentToActivity.intent((Activity) mContext, ProjectSearchActivity.class);
    }


    public String getSearchWords() {
        if (search_input != null) {
            return search_input.getText().toString();
        }
        return "";
    }

}
